package com.example.chaomi;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;


public class TitleAdapter2 extends BaseAdapter {
	List<TitleBean2> mTitles;
	Context mContext;
	int currentposition;
    public TitleAdapter2(Context context) {
    	Log.d("lijh:", "context:TA2"+context.toString());
    	mContext=context;
    	mTitles=getTitles();
    	Log.d("lijh:", "mContext:ta"+mContext.toString());
    }
    
    private List<TitleBean2> getTitles() {
		// TODO Auto-generated method stub
    	if(mTitles!=null&&mTitles.size()>0)
    		return mTitles;
    	FormatDisk formatDisk = new FormatDisk(mContext);
    	List<String> list = formatDisk.getVolumeDescription();
		List<TitleBean2> titles=new ArrayList<>();
		if(list != null)
    	for(int i=0; i<list.size(); i++){
    		TitleBean2 title =new TitleBean2(0,list.get(i));
    		titles.add(title);
    	}
		return titles;
	}

	public void notifyDataSetChanged(int position) {
		// TODO Auto-generated method stub
		
		this.currentposition=position;
		super.notifyDataSetChanged();
	}
	

	@Override
    public int getCount() {
        return mTitles.size();
    }

    @Override
    public Object getItem(int position) {
        return mTitles.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        TitleBean2 title=mTitles.get(position);
        TitleItem titleItem;
        if (convertView == null) {
            View v = LayoutInflater.from(mContext).inflate(R.layout.title_bean, parent,false);

            titleItem = new TitleItem();
            titleItem.mLeft = (ImageView) v.findViewById(R.id.title_left_icon);
            titleItem.mTitle = (TextView) v.findViewById(R.id.title_text);
            titleItem.mRight = (ImageView) v.findViewById(R.id.title_right_icon);
            v.setTag(titleItem);
            convertView = v;
        } else {
            titleItem = (TitleItem) convertView.getTag();
        }
        titleItem.mLeft.setBackgroundResource(title.getLeftres());
        titleItem.mTitle.setText(title.getTitletext());
        if (currentposition==position) {
            titleItem.mRight.setVisibility(View.VISIBLE);
        } else {
        	titleItem.mRight.setVisibility(View.INVISIBLE);
        }
        
        Log.d("567","in return view");
        return convertView;
    }

    static class TitleItem {
        ImageView mLeft;
        TextView mTitle;
        ImageView mRight;
    }
    
}
